package kr.co.redcircle.web.downloader;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class EBookShareNet {
	private String targetUrl;
	private int startNo;
	private int endNo;
	private ArrayList<Integer> errorCollection = new ArrayList<Integer>();
	private int nloop;
	private int connTO;
	private int socketTO;
	private int retryCount;
	private String torrentSavePath;
	private String uTorrentApplicationPath;

	public void setUrl(String url) {
		this.targetUrl = url;
		
	}

	public void startNumber(int sNo) {
		this.startNo = sNo;
	}

	public void endNumber(int eNo) {
		this.endNo = eNo;
	}

	public void get(){
		for (int i=this.startNo; i>this.endNo; i--){
			nloop=0;
			get(i);
		}
	}

	private void get(int getNo) {

	    HttpClient httpclient = new DefaultHttpClient();
	    httpclient.getParams().setParameter("http.protocol.expect-continue", false);
	    httpclient.getParams().setParameter("http.connection.timeout", this.connTO);
	    httpclient.getParams().setParameter("http.socket.timeout", this.socketTO);
	    
	    HttpGet httpget = new HttpGet(targetUrl + getNo);
        HttpResponse response = null;
		try {
			response = httpclient.execute(httpget);
	        HttpEntity entity = response.getEntity();
	        
	        if (entity != null) {
	        	Header[] header = response.getHeaders("Content-Disposition");
	        	String attFile = "";
	        	if (header.length == 1){
	        		attFile = header[0].getValue().replaceAll("attachment; filename=\"", "");
	        		attFile = attFile.replaceAll("\"", "");
	        		String saveFileName = this.torrentSavePath + getNo + "_" + attFile;
	        		
	        		//Create file
	        		OutputStream os = new FileOutputStream( saveFileName );
        			InputStream is = response.getEntity().getContent();
        			byte[] buf = new byte[4096];
        			int read;
        			while ((read = is.read(buf)) != -1) {
        				os.write(buf, 0, read);
        			}
        			os.close();
        			new ProcessBuilder(this.uTorrentApplicationPath, saveFileName).start();
        		
	        		httpget.abort();
	        		httpclient.getConnectionManager().shutdown();
	        		System.out.println(" >> " + saveFileName);
	        	}else{
	        		nloop++;
	        		if (nloop < this.retryCount){
		        		System.out.print(nloop);
		        		get(getNo);
	        		}else{
	        			System.out.println("skip~!");
	        		}        		
	        	}
	        }
	        
		} catch (IOException e) {
			System.out.print("f");
			get(getNo);
		}		
	}

	public void setConnectionTimeOut(int i) {
		this.connTO = i;
	}

	public void setSocketTimeOut(int i) {
		this.socketTO = i;
	}

	public void retryCount(int i) {
		this.retryCount = i;
	}

	public void saveTorrentFilePath(String path) {
		this.torrentSavePath = path;
	}

	public void uTorrentApplcationPath(String path) {
		this.uTorrentApplicationPath = path;
	}
}
